package com.huaxia.finance.consumer.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by mirui on 2016/12/27.
 * 转换与计算工具类，避免空指针异常造成的崩溃
 */

public class ConvertUtils {

    public static String toString(Object obj) {
        String str = "";
        try {
            str = obj.toString();
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getLogutil().d("ConvertUtils.toString : " + e.getMessage());
        }
        return str;
    }

    public static int toInt(Object obj) {
        int num = 0;
        try {
            num = Integer.valueOf(obj.toString());
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getLogutil().d("ConvertUtils.toInt : " + e.getMessage());
        }
        return num;
    }

    public static double toDouble(Object obj) {
        double num = 0;
        try {
            num = Double.parseDouble(obj.toString());
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getLogutil().d("ConvertUtils.toDouble : " + e.getMessage());
        }
        return num;
    }

    public static String mapToString(Map map, String key) {
        String str = "";
        try {
            str = map.get(key).toString();
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getLogutil().d("ConvertUtils.mapToString : " + e.getMessage());
        }
        return str;
    }

    public static int mapToInt(Map map, String key) {
        int num = 0;
        try {
            num = Integer.valueOf(map.get(key).toString());
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getLogutil().d("ConvertUtils.mapToInt : " + e.getMessage());
        }
        return num;
    }

    public static double mapToDouble(Map map, String key) {
        double num = 0;
        try {
            num = Double.parseDouble(map.get(key).toString());
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getLogutil().d("ConvertUtils.mapToInt : " + e.getMessage());
        }
        return num;
    }

    public static List mapToList(Map map, String key) {
        List list = new ArrayList<>();
        try {
            list = (List) map.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getLogutil().d("ConvertUtils.mapToList : " + e.getMessage());
        }
        return list;
    }

    public static Map mapToMap(Map map, String key) {
        Map newMap = null;
        try {
            newMap = (Map) map.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getLogutil().d("ConvertUtils.mapToMap : " + e.getMessage());
        }
        return newMap;
    }

    public static boolean equals(String strO, String strT) {
        boolean isEqauls = false;
        try {
            isEqauls = strO.equals(strT);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getLogutil().d("ConvertUtils.equals : " + e.getMessage());
        }
        return isEqauls;
    }

    public static String sub(String value1, String value2) {
        BigDecimal b1 = null;
        BigDecimal b2 = null;
        String result = null;
        try {
            b1 = new BigDecimal(value1);
            b2 = new BigDecimal(value2);
            result = String.valueOf(b1.subtract(b2).doubleValue());
//            LogUtil.getLogutil().d("subsub  b1 = " + b1 + " ---b2 =  " + b2 + "---result = " + result);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getLogutil().d("ConvertUtils.add : " + e.getMessage());
        }
        return result;
    }

    public static String strFromList(List list,int position){
        String str = "";
        try {
            str = (String) list.get(position);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getLogutil().d("ConvertUtils.strFromList : " + e.getMessage());
        }
        return str;
    }

    public static List listFromList(List list,int position){
        List arrayList = new ArrayList();
        try {
            arrayList = (List) list.get(position);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.getLogutil().d("ConvertUtils.listFromList : " + e.getMessage());
        }
        return arrayList;
    }
}
